package fr.oz.mobile.module.profile.day 
{
	import caurina.transitions.Tweener;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.text.TextField;
	import fr.digitas.flowearth.conf.Conf;
	import fr.oz.core.datas.ProfilDatas;
	import fr.oz.core.datas.ProfilListDatas;
	import fr.oz.core.events.ProfilEvent;
	import fr.oz.core.events.ViewEvent;
	import fr.oz.mobile.common.BGLayer_FC;
	import fr.oz.mobile.module.IModule;
	import fr.oz.mobile.module.IView;
	/**
	 * ...
	 * @author Fazermokeur
	 */
	public class DayProfil extends Sprite implements IModule
	{
		private var copyProfil:ProfilListDatas;
		
		private var step:int = 0;	//O_o
		private var content:IView;
		private var prevContent:IView;
		
		private var _renderProfil:ProfilListDatas;
		
		public function DayProfil(profil:ProfilListDatas) 
		{
			addEventListener(Event.ADDED_TO_STAGE, init);
			
			if (profil != null) {
				copyProfil = new ProfilListDatas(profil._node );
			}else {
				/*	Création d'un nouveau profil	*/
				//copyProfil = new ProfilListDatas(Conf.getDatas("NEW PROFIL"));
			}
			
			
		}
		
		private function init(e:Event):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			addEventListener(Event.REMOVED_FROM_STAGE, dispose);
			
			var hourSelector:HourSelector = new HourSelector(copyProfil);
			hourSelector.addEventListener(Event.COMPLETE, handleViewComplete);
			addChild(hourSelector);
			content = hourSelector;
			dispatchEvent(new ViewEvent(ViewEvent.VIEW_CREATED, content.title, content.cachePosY));
		}
		
		private function handleViewComplete(e:Event):void 
		{
			copyProfil = (e.currentTarget as IView).getValue();
			switch(step) {
				case 0:
					//couleur
					var color:ColorSelector = new ColorSelector(copyProfil);
					switchView(color);
				break;
				case 1:
					//music
					var music:MusicSelector = new MusicSelector(copyProfil);
					switchView(music);
				break;
				case 2:
					//name
					var name:ProfilValidator = new ProfilValidator(copyProfil);
					switchView(name);
				break;
				case 3:
					//finish
					_renderProfil = copyProfil;
					dispatchEvent(new ProfilEvent(ProfilEvent.PROFIL_CHANGED, copyProfil));
				break;
			}
			step++;
		}
		
		private function switchView(m:IView):void
		{
			if (content) {
				if (prevContent != null) {
					if (contains(prevContent as DisplayObject))	removeChild(prevContent as DisplayObject);
					(content as EventDispatcher).removeEventListener(Event.COMPLETE, handleViewComplete);
					prevContent = null;
				}
				prevContent = content;
				content = m;
				Tweener.addTween(prevContent, {alpha:0, time:1, onComplete:contentSwitched } );
			}else {
				content = m;
			}
			addChildAt((content as DisplayObject), 0);
			(content as DisplayObject).alpha = 0;
			(content as EventDispatcher).addEventListener(Event.COMPLETE, handleViewComplete);
			Tweener.addTween(content, { alpha:1, time:1 } );
			dispatchEvent(new ViewEvent(ViewEvent.VIEW_CREATED, content.title, content.cachePosY));
		}
		
		private function contentSwitched():void
		{
			removeChild(prevContent as DisplayObject);
		}
		
		private function dispose(e:Event):void 
		{
			removeEventListener(Event.REMOVED_FROM_STAGE, dispose);
			
		}
		
		
		/* INTERFACE fr.oz.mobile.module.IModule */
		
		public function resize():void
		{
			
		}
		
		/**
		 * animation d'appartition
		 */
		public function activeView():void
		{
			
		}
		
		/**
		 * animation de dissipation
		 */
		public function removeView():void
		{
			
		}
		
		public function goBack():Boolean
		{
			return false;
		}
		
		public function get renderProfil():ProfilListDatas { return _renderProfil; }
	}

}